package com.woniuxy.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.dto.MenusDto;
import com.woniuxy.dto.goodsGeneralAndSmallAndBrand.GoodsSmallerDto;
import com.woniuxy.entity.MenuTable;
import com.woniuxy.entity.RoleMenu;
import com.woniuxy.entity.RoleTable;
import com.woniuxy.entity.UserRole;
import com.woniuxy.enums.CodeEnum;
import com.woniuxy.exception.PawnMenuException;
import com.woniuxy.exception.PawnRoleException;
import com.woniuxy.mapper.RoleMenuMapper;
import com.woniuxy.param.UserRelatedParam.SaveRoleMenusParam;
import com.woniuxy.service.MenuTableService;
import com.woniuxy.service.RoleMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.service.RoleTableService;
import com.woniuxy.service.UserRoleService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zcx, ftl, gr, rhs
 * @since 2021-12-31
 */
@Service
public class RoleMenuServiceImpl extends ServiceImpl<RoleMenuMapper, RoleMenu> implements RoleMenuService {
    @Resource
    private RoleTableService rts;

    @Resource
    private MenuTableService mts;

    @Resource
    private UserRoleService urs;


    @Resource
    private RedisTemplate<String, List<MenusDto>> rt;


    @Override
    public boolean saveRoleMenus(SaveRoleMenusParam arm) {
        //判断角色是否存在
        RoleTable roleTable = rts.getById(arm.getRoleId());
        if (StringUtils.isEmpty(roleTable)) throw new PawnRoleException(CodeEnum.ROLE_NULL.getMessage(), CodeEnum.ROLE_NULL.getCode());
        // 删除所有redis
        List<UserRole> userRoles = urs.list(new QueryWrapper<UserRole>().eq("role_id", arm.getRoleId()));
        for (UserRole userRole : userRoles) {
            List<MenusDto> range = rt.opsForValue().get("pawn:menu:userId" + userRole.getUserId());
            if (null == range || range.size() <= 0) {
                continue;
            }
            rt.delete("pawn:menu:userId" + userRole.getUserId());
        }


        //清空角色账号
        remove(new QueryWrapper<RoleMenu>().eq("role_id", arm.getRoleId()));



        //如果没勾选就可以返回了
        if (arm.getCheckIds() == null || arm.getCheckIds().size() == 0) return true;
        //获取所有菜单
        List<MenuTable> menus = mts.list(new QueryWrapper<MenuTable>().in("menu_id", arm.getCheckIds()));
        if (menus.size() != arm.getCheckIds().size()) throw new PawnMenuException(CodeEnum.MENU_ERROR_SIZE.getMessage(), CodeEnum.MENU_ERROR_SIZE.getCode());

        List<RoleMenu> list = arm.getCheckIds().stream().map(
                id -> {
                    RoleMenu wrs = new RoleMenu();
                    wrs.setRoleId(arm.getRoleId());
                    wrs.setMenuId(id);
                    return wrs;
                }).collect(Collectors.toList());
        return saveBatch(list);
    }
}
